package cn.fxl.pet.controller;

import cn.fxl.basic.exception.BusinessExciption;
import cn.fxl.basic.pageList.GmResult;
import cn.fxl.basic.result.AjaxResult;
import cn.fxl.pet.domain.SearchMasterMsg;
import cn.fxl.pet.query.SearchMasterMsgQuery;
import cn.fxl.pet.service.ISearchMasterMsgService;
import com.baomidou.mybatisplus.core.metadata.PageList;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody Long [] ids){
        try {
                searchMasterMsgService.pathchdel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public SearchMasterMsg getOne(@PathVariable("id")Long id) {
        return searchMasterMsgService.getOne(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return searchMasterMsgService.getAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public GmResult<SearchMasterMsg> Pagequery(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.Pagequery(query);
    }
    @PostMapping("/publish")
    public AjaxResult Pagequery(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        try {
         return  searchMasterMsgService.publish(searchMasterMsg,request);
        } catch (BusinessExciption businessExciption) {
            businessExciption.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(businessExciption.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }
    }
    @PostMapping("/user")
    public GmResult<SearchMasterMsg> userPagequery(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.userPagequery(query,request);
    }
    @PostMapping("/toAudit")
    public GmResult<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query) {
        query.setState(0);
        return searchMasterMsgService.Pagequery(query);
    }
    @PostMapping("/finish")
    public GmResult<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query) {
        query.setState(2);
        return searchMasterMsgService.Pagequery(query);
    }
    @PostMapping("/toHandle")  ///searchMasterMsg/toHandle
    public GmResult<SearchMasterMsg> ToHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        query.setState(1);
        return searchMasterMsgService.Tohandle(query,request);
    }
    @GetMapping(value ="/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id){
        try {
             searchMasterMsgService.reject(id);
             return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("拒单失败");
        }
    }
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody Map<String,String> map) {
        try {
            searchMasterMsgService.accept(map);
            return AjaxResult.me();
        } catch (BusinessExciption e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }
    }
}
